﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EvidenceManagement.Model;
using EvidenceManagement.Model.Entity;
using EvidenceManagement.Logic.Security;
using EvidenceManagement.Model.Security;
using EvidenceManagement.PresentationModel.Common;
using EvidenceManagement.ViewModel.Common;
using EvidenceManagement.ViewModel.Security;

namespace EvidenceManagement.PresentationModel.Security
{
    public class RegisterUserPresenter : IRegisterUserPresenter
    {
        #region Fields

        readonly SystemUser user = null;
        readonly IRegisterUserView view = null;

        #endregion

        #region Constructor

        private RegisterUserPresenter()
        {
        }

        public RegisterUserPresenter(IRegisterUserView registerUserView)            
        {
            view = registerUserView;

            user = new SystemUser();
        }

        #endregion

        #region IPresenter Members

        public IView View
        {
            get { return (view as IView); }
        }

        public void OnLoad()
        {
        }

        public void OnClose()
        {            
        }

        #endregion

        public void AddPersonalDetails()
        {
            //Employee
            user.Employee.FirstName = view.FirstName;
            user.Employee.MiddleName = view.MiddleName;
            user.Employee.Surname = view.Surname;
            user.Employee.Initials = GetInitials();
            user.Employee.IdentityNumber = view.IdentityNumber;
            user.Employee.PersonID = Guid.Empty;
            user.Employee.EmployeeNumber = view.EmployeeNumber;
            user.Employee.DateOfBirth = view.DateOfBirth;
            user.Employee.Age = CalculateAge();

            
        }

        #region Private Methods

        private string GetInitials()
        {
            return string.Format("{0}{1}", ((view.FirstName.Length > 1) ? view.FirstName.Substring(0, 1) + "." : string.Empty), ((view.MiddleName.Length > 1) ? view.MiddleName.Substring(0, 1) + "." : string.Empty));
        }

        private int CalculateAge()
        {
            int ageFromDOB;
            ageFromDOB = (DateTime.Now.Year - view.DateOfBirth.Year) + (DateTime.Now.Month > view.DateOfBirth.Month ? -1 : 0);

            return ageFromDOB;
        }

        #endregion
    }
}
